package com.base.system.managedBean;

import java.io.Serializable;
import java.util.Map;

import javax.faces.context.FacesContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.operamasks.faces.annotation.Action;
import org.operamasks.faces.annotation.BeforeRender;
import org.operamasks.faces.annotation.Bind;
import org.operamasks.faces.annotation.Inject;
import org.operamasks.faces.annotation.ManagedBean;
import org.operamasks.faces.annotation.ManagedBeanScope;
import org.operamasks.faces.component.layout.impl.UIPanel;
import org.operamasks.faces.user.util.Browser;

import com.base.security.SecurityConst;
import com.base.system.managedBean.baseBean.BaseCommonKeyBean;

/**
 * 
 * This managed bean is generated automatically
 */
@ManagedBean(name = "system_showCommonKeyBean", scope = ManagedBeanScope.REQUEST)
public class ShowCommonKeyBean extends BaseCommonKeyBean implements
		Serializable {
	private final static Log logger = LogFactory
			.getLog(ShowCommonKeyBean.class);
	@Inject
	FacesContext context;

	@BeforeRender
	public void beforeRender(boolean isPostback) {
		if (!isPostback) {

			Map<String, Object> session = context.getExternalContext()
					.getSessionMap();

			// commonKey = (CommonKey) session
			// .get(SecurityConst.COMMONKEY_SELECT_ROW_DATA);
			// if (commonKey == null) {
			id = context.getExternalContext().getRequestParameterMap().get(
					"commonKeyId");
			commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));
			// }

			if (commonKey.isCheckEnable()) {
				styleNo = "display:block";
				styleYes = "display:none";
			} else {
				styleNo = "display:none";
				styleYes = "display:block";
			}

			this.getCommonKeyProperty();
		}

	}

	@Action(id = "remove")
	public void removeCommonKey() {

		if (commonKey == null) {
			commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));
		}
		// System.out.println("删除：" + notice);
		commonKeyDao.delete(commonKey);
		// String script = "closeCenterLayoutTab('"
		// + SecurityConst.COMMONKEY_SHOW_TAB + "')";
		// Browser.execClientScript("window.top", script);

		Browser
				.execClientScript("window.parent.gridReloadJsvar.fireEvent('click');");
	}

	@Action(id = "copeyCreate")
	public void copeyCreate() {
		Browser
				.execClientScript("window.parent.createJsvar.fireEvent('click');");
	}

	@Action(id = "editCommonKey")
	public void editCommonKey() {

		// if (commonKey == null) {
		// // id = context.getExternalContext().getRequestParameterMap().get(
		// // "commonKeyId");
		// commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));
		// }
		// context.getExternalContext().getSessionMap().put(
		// SecurityConst.COMMONKEY_SELECT_ROW_DATA, commonKey);
		//
		// String url = "/modules/system/webadmin/editCommonKey.faces";
		// url = context.getExternalContext().getRequestContextPath() + url;
		// String script = "addCenterLayoutTab('"
		// + SecurityConst.COMMONKEY_EDIT_TAB + "','" + "编辑配置项" + "', '"
		// + url + "')";
		// Browser.execClientScript("window.top", script);
		// String scriptShow = "closeCenterLayoutTab('"
		// + SecurityConst.COMMONKEY_SHOW_TAB + "')";
		// Browser.execClientScript("window.top", scriptShow);
		Browser.execClientScript("window.parent.editJsvar.fireEvent('click');");
	}

	@Bind(id = "styleYes", attribute = "value")
	public String styleYes = "display:none";

	@Bind(id = "styleNo", attribute = "value")
	public String styleNo = "display:none";

	@Bind(id = "mainShowCK")
	private UIPanel mainShowCK;

	@Action(id = "yesEnabledExpert")
	public void yesEnabledExpert() {

		commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));

		context.getExternalContext().getSessionMap().put(
				SecurityConst.COMMONKEY_SELECT_ROW_DATA, commonKey);

		if (commonKey == null)
			return;
		commonKey.setEnabled(true);
		commonKeyDao.saveOrUpdate(commonKey);

		styleNo = "display:block";
		styleYes = "display:none";

		this.getCommonKeyProperty();
		mainShowCK.repaint();
	}

	@Action(id = "noEnabledExpert")
	public void noEnabledExpert() {

		commonKey = commonKeyService.getCommonKeyById(Integer.parseInt(id));

		context.getExternalContext().getSessionMap().put(
				SecurityConst.COMMONKEY_SELECT_ROW_DATA, commonKey);

		if (commonKey == null)
			return;
		commonKey.setEnabled(false);
		commonKeyDao.saveOrUpdate(commonKey);

		styleNo = "display:none";
		styleYes = "display:block";
		this.getCommonKeyProperty();
		mainShowCK.repaint();
	}
}
